﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace NetWatch
{
    public partial class MainForm : Form
    {
        public static MainForm Current = null;

        NotifyIcon ni;

        public MainForm()
        {
            Current = this;

            InitializeComponent();

            ni = new NotifyIcon();
            ni.BalloonTipTitle = Application.ProductName;
            ni.Icon = new Icon(SystemIcons.Asterisk, 40, 40);
            ni.Visible = true;
            ni.Text = Application.ProductName;
            ni.DoubleClick += new EventHandler(ni_DoubleClick);

            this.Visible = false;
            this.ShowInTaskbar = false;
            this.Hide();
            this.WindowState = FormWindowState.Minimized;

            this.Resize += new EventHandler(MainForm_Resize);

            LoadTests();
        }

        void MainForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.Hide();
        }

        void tp_StateChanged(object obj)
        {
            ShowNotif(obj);
        }

        void ni_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            //this.ShowInTaskbar = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseApplication();
        }

        public void CloseApplication()
        {
            this.ni.Visible = false;

            this.Close();
        }

        public delegate void AddItemDelegate(object obj);
        public void AddItem(object obj)
        {
            if (this.listBox1.InvokeRequired)
                this.listBox1.Invoke((MethodInvoker)delegate { AddItem(obj); });
            else
            {
                this.listBox1.Items.Add(obj);
                this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
                this.listBox1.SelectedIndex = -1;
            }
        }


        public void ShowNotif(object obj)
        {
            ni.BalloonTipText = obj.ToString();
            ni.ShowBalloonTip(2000);
        }


        private void LoadTests()
        {
            Dictionary<string, object> configTP = new Dictionary<string,object>();
            configTP.Add("Host", "10.2.12.5");
            configTP.Add("PeriodOk", 5);
            configTP.Add("PeriodError", 2);

            NetWatch.ATest.ATest tp = new Tests.TestPing.TestPing();
            tp.LoadConfig(configTP);
            tp.TestStateChanged += new ATest.TestStateChangedDelegate(TestStateChanged);
            tp.TestFinished += new ATest.TestStateChangedDelegate(TestFinished);
            tp.Start();
        }

        void TestFinished(ATest.ATest sender, ATest.ETestState state)
        {
            AddItem(string.Format("{0}: {1}", sender.Info, state));
        }

        void TestStateChanged(ATest.ATest sender, ATest.ETestState state)
        {
            this.ni.BalloonTipText =
                string.Format("{0}: {1}", sender.Info, state);
            this.ni.ShowBalloonTip(2000);
        }
    }
}
